package com.ssm.wechat.handler;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class WeChatMsgFactory implements InitializingBean {

    @Resource
    private List<WeChatMsgHandler> weChatMsgHandlerList;

    private Map<WeChatMsgTypeEnum, WeChatMsgHandler> msgHandlerMap = new HashMap<>();

    public WeChatMsgHandler getHandlerByMsgType(String msgType) {
        WeChatMsgTypeEnum msgTypeEnum = WeChatMsgTypeEnum.getByMsgType(msgType);
        return msgHandlerMap.get(msgTypeEnum);
    }

    /**
     * 初始化完当前Bean后执行(为map赋值)
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        for(WeChatMsgHandler weChatMsgHandler : weChatMsgHandlerList) {
            msgHandlerMap.put(weChatMsgHandler.getMsgType(), weChatMsgHandler);
        }
    }
}
